package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.views.components.ContentMiddle;
	import com.rogo.rogobox.views.components.consts.ContentMiddleConst;
	
	public class ContentMiddelMediator extends Mediator
	{
		public function ContentMiddelMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'contentMiddleMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var history:Array = [];
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():ContentMiddle
		{
			return viewComponent as ContentMiddle;
		}
		
		private function recordHistory(state:String):void
		{
			removeHistory(state);
			history.push(state);
		}
		
		private function removeHistory(state:String):void
		{
			for (var i:int = 0; i < history.length; i++)
			{
				if (history[i] == state)
					history.splice(i, 1);
			}
		}
		
		private function back():void
		{
			if (history.length == 0)
				return;
			
			var state:String = history.pop();
			view.currentState = state;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.CHANGE_CONTENT_VIEW,
				AppFacade.BACK_CONTENT_VIEW];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			if (notification.name == AppFacade.CHANGE_CONTENT_VIEW)
			{
				switch(notification.param)
				{
					case ContentMiddleConst.VIEW_FRIEND:
						recordHistory(view.currentState);
						view.currentState = ContentMiddleConst.VIEW_FRIEND;
						removeHistory(view.currentState);
						break;
					case ContentMiddleConst.VIEW_PROFILE:
						recordHistory(view.currentState);
						view.currentState = ContentMiddleConst.VIEW_PROFILE;
						removeHistory(view.currentState);
						break;
					case ContentMiddleConst.VIEW_MAIN:
						recordHistory(view.currentState);
						view.currentState = ContentMiddleConst.VIEW_MAIN;
						removeHistory(view.currentState);
						break;
					default:
						view.currentState = ContentMiddleConst.VIEW_HOME;
						history = [];
				}
			}
			else
			{
				back();
			}
		}
		
	}
}